package client.control;

import base.BaseValue;
import client.entity.Good;
import client.entity.Store;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * @Description 商铺有关的网络接口封装
  * @author 孔明浩
  * @Date 2020/4/8
  */
public class StoreControl {
    StoreControl(){};
    public ResponseEntity createStore(String token, String storeName, String description){
        ConnectControl connectControl = new ConnectControl();
        Map<String ,String> map = new HashMap<>();
        map.put("storeName",storeName);
        map.put("description", description);
        connectControl.doPost(BaseValue.load + "createStore", token, map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity getStoreInfo(String token){
        ConnectControl connectControl = new ConnectControl();
        connectControl.doPost(BaseValue.load + "getStoreInfo", token, null);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity setStoreInfo(String token, String storeName, String description){
        ConnectControl connectControl = new ConnectControl();
        Map<String ,String> map = new HashMap<>();
        map.put("storeName",storeName);
        map.put("description", description);
        connectControl.doPost(BaseValue.load + "setStoreInfo", token, map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity addStoreCollection(String token, int storeId){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("storeId", storeId + "");
        connectControl.doPost(BaseValue.load + "addStoreCollection",token,map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity deleteStoreCollection(String token, int storeId){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("storeId", storeId + "");
        connectControl.doPost(BaseValue.load + "deleteStoreCollection",token,map);
        return ResponseEntity.fit(connectControl);
    }

    public ResponseEntity getUserIdByStoreId(int storeId){
        ConnectControl connectControl = new ConnectControl();
        Map<String, String> map = new HashMap<>();
        map.put("storeId", storeId + "");
        connectControl.doPost(BaseValue.load + "getUserIdByStoreId", null, map);
        return ResponseEntity.fit(connectControl);
    }

    public List<Good> getStoreGoods(String token){
        ResponseEntity responseEntity = getStoreInfo(token);
        List<Good> goods = new ArrayList<>();
        if (responseEntity.getCode() == 0){
            Store store = JSONObject.parseObject(responseEntity.getData(), Store.class);
            List<Integer> list = JSONObject.parseArray(store.getGood(), Integer.class);
            for (int l:list){
                ResponseEntity responseEntity1 = Controller.GOOD_CONTROL.getGoodInfo(l);
                if (responseEntity1.getCode() == 0){
                    goods.add(JSONObject.parseObject(responseEntity1.getData(), Good.class));
                }
            }
        }
        return goods;
    }


}
